package com.vvv.designpattern.synchronizedpattern;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author vvv
 * @date 2023-10-17 08 20
 * discription 固定运行顺序之wait notify版
 * 先输出2 再输出1
 */
public class FixedReentrantLock {
    public static final ReentrantLock lock = new ReentrantLock();
    public static final Condition condition = lock.newCondition();
    public static boolean is2Ran = false;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                while (!is2Ran) {
                    try {
                       condition.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println(1);
            } finally {
                lock.unlock();
            }
    },"t1");
    Thread t2 = new Thread(() -> {
        lock.lock();
        try {
            System.out.println(2);
            is2Ran = true;
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }, "t2");
        t1.start();
        Thread.sleep(1000);
        t2.start();

}
}
